package cc.verywell.pureblock.msg.nfs.map;

import java.nio.file.Path;

import cc.verywell.pureblock.func.node.KeyShell;

/**
* 
* Copyright © 2019 TanYaqiu. All rights reserved.
* @author TanYaqiu 
* @address flat 601
* @date 2019年4月20日 下午9:38:27
*/
public class JOINmap implements FSmapHolding {
	private MEMmap mem;
	private HDmapHolding hd;

	public JOINmap(long memcapacity,Path hdPath,long hdcapacity) {
		mem = new MEMmap(memcapacity);
		hd = new HDmapHolding(hdPath, hdcapacity);
	}

	@Override
	public boolean put(KeyShell key, byte[] bb) {
		if(mem.put(key, bb)) {
			return hd!=null?hd.put(key,bb):true;
		}else{
			return false;
		}
	}

	@Override
	public boolean remove(KeyShell key) {
		mem.remove(key);
		if(hd!=null)hd.remove(key);
		return true;
	}

	@Override
	public byte[] get(KeyShell key) {
		byte[] bb = mem.get(key);
		if(bb==null) {
			if(hd!=null)bb = hd.hdget(key);
			if(bb==HDmap.ZERObuffer) {
				bb=null; //TODO 访问下一层;
			}
			if(bb!=null) {
				mem.put(key,bb);
			}
		}
		return bb;
	}
	
	@Override
	public boolean contain(KeyShell key) {
		if(mem.contain(key))return true;
		return hd.contain(key);
	}
	
	@Override
	public void addHold(Object lock, KeyShell... keys) {
		hd.addHold(lock, keys);
	}
	@Override
	public void removeHold(Object lock) {
		hd.removeHold(lock);
	}
	@Override
	public int holdSize() {
		return hd.holdSize();
	}
	@Override
	public long capacity() {
		return hd.capacity();
	}
	

}
